﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 点集筛选器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            MinimumSize = Size;
            MaximumSize = Size;
        }

        private Graphics g;
        private Matrix matrix;
        private List<Point> points;
        private Point Start;
        private Point End;
        private Bitmap bitmap;
        private Pen pen;
        private int r;

        private void InitGraphics()
        {
            bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g = Graphics.FromImage(bitmap);
            matrix = new Matrix();
            matrix.Translate(g.VisibleClipBounds.Width / 2, g.VisibleClipBounds.Height / 2);
            matrix.Scale(1, -1);
            g.Transform = matrix;
            pen = new Pen(Color.Black, 1);
        }

        private void button_paint_Click(object sender, EventArgs e)
        {
            int number = int.Parse(txtNumber.Text.Trim());
            Random rnd = new Random();
            InitGraphics();
            r = int.Parse(txtR.Text);

            string[] start = txtStart.Text.Trim(new char[] { '（', '）', ')', '(', ' ' })
                    .Split(new char[] { ',', '，' }, StringSplitOptions.RemoveEmptyEntries);
            Start = new Point(int.Parse(start[0]),int.Parse(start[1]));

            string[] end = txtEnd.Text.Trim(new char[] { '（', '）', ')', '(', ' ' })
                .Split(new char[] { ',', '，' }, StringSplitOptions.RemoveEmptyEntries);
            End = new Point(int.Parse(end[0]), int.Parse(end[1]));

            g.DrawLine(pen, Start, End);
            points = new List<Point>();

            for (int i = 0; i < number; i++)
            {
                int x = rnd.Next(-bitmap.Width / 2, bitmap.Width / 2);
                int y = rnd.Next(-bitmap.Height / 2, bitmap.Height / 2);
                points.Add(new Point(x, y));
                g.DrawEllipse(pen, x - r, y - r, r * 2, r * 2);
            }

            pictureBox1.Image = bitmap;
        }

        private void button_screen_Click(object sender, EventArgs e)
        {
            List<Point> leftPoints = new List<Point>();
            if (Start.Y == End.Y)
            {
                foreach (Point point in points)
                {
                    if(point.Y > Start.Y)
                    {
                        leftPoints.Add(point);
                    }
                }
            }else if (End.X == Start.X)
            {
                foreach(Point point in points)
                {
                    if (point.X < End.X)
                    {
                        leftPoints.Add(point);
                    }
                }
            }
            else
            {
                double k = (double)(Start.Y - End.Y) / (Start.X - End.X);
                double b = Start.Y - Start.X * k;
                foreach (Point point in points)
                {
                    if(point.X < (double)(point.Y-b)/k)
                    {
                        leftPoints.Add(point);
                    }
                }
            }
            InitGraphics();
            
            for (int i = 0; i < leftPoints.Count; i++)
            {
                g.DrawEllipse(pen, leftPoints[i].X - r, leftPoints[i].Y - r, 2 * r, 2 * r);
            }

            g.DrawLine(pen, Start, End);
            pictureBox1.Image = bitmap;
        }

        private void button_screenRight_Click(object sender, EventArgs e)
        {
            List<Point> RightPoints = new List<Point>();
            if (Start.Y == End.Y)
            {
                foreach (Point point in points)
                {
                    if (point.Y < Start.Y)
                    {
                        RightPoints.Add(point);
                    }
                }
            }
            else if (End.X == Start.X)
            {
                foreach (Point point in points)
                {
                    if (point.X > End.X)
                    {
                        RightPoints.Add(point);
                    }
                }
            }
            else
            {
                double k = (double)(Start.Y - End.Y) / (Start.X - End.X);
                double b = Start.Y - Start.X * k;
                foreach (Point point in points)
                {
                    if (point.X > (double)(point.Y - b) / k)
                    {
                        RightPoints.Add(point);
                    }
                }
            }
            InitGraphics();

            for (int i = 0; i < RightPoints.Count; i++)
            {
                g.DrawEllipse(pen, RightPoints[i].X - r, RightPoints[i].Y - r, 2 * r, 2 * r);
            }

            g.DrawLine(pen, Start, End);
            pictureBox1.Image = bitmap;
        }

        private void button_sameDirection_Click(object sender, EventArgs e)
        {
            InitGraphics();
            List<Point> Samepoints = new List<Point>();
            int x2 = End.X - Start.X;
            int y2 = End.Y - Start.Y;
            foreach (var point in points)
            {
                int x1 = point.X - Start.X;
                int y1 = point.Y - Start.Y;
                int result = x1 * x2 + y1 * y2;
                if(result > 0)
                {
                    Samepoints.Add(point);
                }
            }
            for (int i = 0; i < Samepoints.Count; i++)
            {
                g.DrawEllipse(pen, Samepoints[i].X - r, Samepoints[i].Y - r, 2 * r, 2 * r);
            }

            g.DrawLine(pen, Start, End);
            pictureBox1.Image = bitmap;
        }

        private void button_differentDirection_Click(object sender, EventArgs e)
        {
            InitGraphics();
            List<Point> Diffoints = new List<Point>();
            int x2 = End.X - Start.X;
            int y2 = End.Y - Start.Y;
            foreach (var point in points)
            {
                int x1 = point.X - Start.X;
                int y1 = point.Y - Start.Y;
                int result = x1 * x2 + y1 * y2;
                if (result < 0)
                {
                    Diffoints.Add(point);
                }
            }
            for (int i = 0; i < Diffoints.Count; i++)
            {
                g.DrawEllipse(pen, Diffoints[i].X - r, Diffoints[i].Y - r, 2 * r, 2 * r);
            }

            g.DrawLine(pen, Start, End);
            pictureBox1.Image = bitmap;
        }
    }
}
